class Player
  attr_reader :x, :y, :score, :lives, :speed, :radius, :died, :hp
  def initialize(animation, x, y, speed, radius)
    #animation = picture file
    #x = starting x coordiante
    #y = y coordinate
    #speed = speed of player's spacecraft when navigating about the screen
    #radius = approx radius (even though it is not circular) of ship for collision detection with projectiles, enemies, and other barriers
    @animation = animation
    @x, @y = x, y
    @a = 3
    @b = 0
    @img = @animation[@a]
    #@move_x = 0
    @firing = false
    @turn_right = false
    @turn_left = false
    @count = 0
    #@count_left = 0
    #@count_right = 0
    @score = 00000000
    @lives = 5
    @speed = speed
    @radius = 20
    @hasfired = false
    @died = false
    @hp = 10
    @dead_count = 0
    @font = Gosu::Font.new($screen, Gosu::default_font_name, 30)
    @shield = false
    @count_shield = 0
    @image = Images['media/shield.png', false]
    @halfwidth = 20
    @halfheight = 23
    @color = Gosu::Color.new(255, 255, 255, 255)
  end

  def strafe_left
    @x = [@x - @speed, 140].max
    @turn_left = true
  end
  
  def strafe_right
    @x = [@x + @speed, 500].min
    @turn_right = true
  end
  
  def move_up
    @y = [@y - @speed, 25].max
  end
  
  def move_down
    @y = [@y + @speed, 455].min
  end
  
  
  
  #sets the state of the player firing to true. used if spacebar is pressed and a shot is fired
  #so in update it can choose a sprite (tile of image) that is in the act of "firing"
  def jet_fire
    @firing = true
  end
  
  #increase the score of the player by amount
  def increase_score(amount)
    @score += amount
  end
  
  #In player's case, update chooses which animation of media/jet.bmp will be shown based on the movement of the aircraft and if it is firing or not
  #a bit confusing, but should be more straightforward once i get more tiles from kevin
  #returns true if @lives == 0 to change event trigger in gamewindow
  def update
    
    if @died
      @dead_count += 1
    end
    
    if @dead_count == 50
      @died = false
      @dead_count = 0
      @hp = 10
      warp($game.screen_width / 2.0, $game.screen_height - 75)
    end
    
    if @shield
      @count_shield += 1
    end
    
    if @count_shield == 20
      @shield = false
      @count_shield = 0
    end
    
    
    if @turn_right then
      if @a < 6 and @b % 4 == 0 and @count == 0
        @a += 1
        @b = 0
      end
      if @a < 13 and @a > 7 and @firing and @b % 4 == 0
        @a+=1
        @b=0
      end
      if @firing
        @count += 1
        if !@hasfired
          @a += 7
          @hasfired = true
        end
     end
     @b+=1
    
      
      
      
    elsif @turn_left then
      if @a > 0 and @count == 0 and @b % 4 == 0
        @a -= 1
        @b = 0
      end
      if @a < 13 and @a > 7 and @firing and @b % 4 == 0
        @a -=1
        @b = 0
      end
      if @firing
        @count += 1
        if !@hasfired
          @a += 7
          @hasfired = true
        end
      end
      @b += 1
      
    else
      if @firing
        @count += 1
        if !@hasfired
          @a += 7
          @hasfired = true
        end
      end
      if @a > 3 and @b % 6 == 0 and @count == 0
         @a -= 1
         @b = 0
       elsif @a < 13 and @a > 10 and @firing and @b % 6 == 0
        @a -=1
        @b = 0
      end
      if @a < 3 and @b % 6 == 0 and @count == 0
         @a +=1
         @b = 0
       elsif @a < 10 and @a > 6 and @firing and @b % 6 == 0
        @a+=1
        @b=0
      end
      @b += 1
     end
     @img = @animation[@a]
    
    #will show a "firing" animation for 5 cycles so that it can be seen, otherwise its just a brief flicker
    if @count == 5
      @firing = false
      @hasfired = false
      @count = 0
      @a -= 7
    end
    @turn_right = false
    @turn_left = false
    if @lives == 0
      return true
    else
      return false
    end
  end
  
  def warp(newx, newy)
    @x = newx
    @y = newy
  end
  
  
  #detects collision between the player and all enemies or non-player projectiles on screen
  #returns true if there is a collision, though this is not needed anymore
  def collision(enemies, projectiles, explode, particles, hit_sound)
    coll = false
    enemies.reject! do |enemy|
       #@collx = false
       #@colly = false
       #@temp = enemy.x + enemy.halfwidth - @x - @halfwidth 
       #@temp2= enemy.x - enemy.halfwidth - @x + @halfwidth 
       #@temp3 = enemy.y + enemy.halfheight - @x - @halfheight
       #@temp4 = enemy.y - enemy.halfheight - @y + @halfheight
        #if (-1 < @temp and @temp < 1) or (-1 < @temp2 and @temp2 < 1)
         # @collx = true
        #end
        #if (-1 < @temp3 and @temp3 < 1) or (-1 < @temp4 and @temp4 < 1) 
         # @colly = true
        #end
      if Gosu::distance(@x, @y, enemy.x, enemy.y) < @radius + enemy.radius then
        #if @collx and @colly
        @score += enemy.score
        @lives -= 1
        @hp = 0
        SDL::Mixer.setVolume(-1, 30)      
        SDL::Mixer.playChannel(-1, explode, 0)
        50.times {particles.push( Particle.new(ZOrder::Explosion, rand(400) + 120 ,@y, 'media/explosion.png', 0, 0, rand(360), 0, 2.0, 0.05))}
        #50.times {particles.push( Particle.new(@x ,rand(480), 'media/explosion.png', 0, 0, rand(360), 0, 2.0, 0.05))}
        5.times {particles.push( Particle.new(ZOrder::Explosion, enemy.x ,enemy.y, 'media/explosion.png', 0, 0, rand(360), 0, 1.0, 0.05))}
        @died = true
        warp(20000, 0)
        true
        coll = true
      elsif(enemy.y > 480 + 2 * enemy.radius)
        true
      else
        false
      end
    end
  projectiles.reject! do |projectile|
    if Gosu::distance(@x, @y, projectile.x, projectile.y) < 20 and !projectile.player_projectile then
      @hp -= projectile.damage
      if @hp <= 0
      SDL::Mixer.setVolume(-1, 30)      
      SDL::Mixer.playChannel(-1, explode, 0)
      #5.times {particles.push( Particle.new(@x ,@y, 'media/explosion.png', 0, 0, rand(360), 0, 2.0, 0.05))}
      @died = true
      @hp = 0
      @lives -= 1
      50.times {particles.push( Particle.new(ZOrder::Explosion, rand(400) + 120 ,@y, 'media/explosion.png', 0, 0, rand(360), 0, 2.0, 0.05))}
      warp(20000, 0)
      #explosions.push(Explosion.new(explosion, projectile.x, projectile.y, @player))
      true
      coll = true
      elsif @hp > 0
       SDL::Mixer.setVolume(-1, 20)      
       SDL::Mixer.playChannel(-1, hit_sound, 0)
        @shield = true
        coll = true
      end
    else
      false
    end
  end
  return coll
  end
  
  #draw player's ship... should probably convert to draw_rot
  def draw
    #@font.draw_rel("Dead Count:#{@dead_count}", $game.screen_width / 2.0, $game.screen_height / 2.0, ZOrder::UI, 0.5, 0.5, 1.0, 1.0, 0xffcccccc)
    if(!@died)
    @img.draw_rot(@x, @y, ZOrder::Player,0, 0.5, 0.5, 1, 1, @color)
  end
  
    if @shield and Gosu::milliseconds % 2 == 0
      @image.draw(@x - @image.width/2.0, @y - @image.height / 2.0, ZOrder::Player)
    end
    
  
  end
end